﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using HitachiLib.Data;
using HitachiLib.HitachiServiceRef;
using HitachiLib.Utils;

namespace HitachiApp.Model
{
    public class ScheduleUnitTimeControlModel : ScheduleUnitBaseModel, INotifyPropertyChanged
    {
        #region Member
        DateTime dt;
        ObservableCollection<ViewItem> schedules = new ObservableCollection<ViewItem>();
        int action_type;
        ScheduleTabViewModel parent;
        #endregion

        #region Properties
        public ObservableCollection<ViewItem> UnitTimeProjects
        {
            get { return schedules; }
            set { schedules = value; }
        }

        public int Type
        {
            get { return action_type; }
        }
        public DateTime StartTime
        {
            get { return dt; }
        }
        #endregion

        public ScheduleUnitTimeControlModel(DateTime datetime, int type, ScheduleTabViewModel parent_model):base(type)
        {
            parent = parent_model;
            dt = datetime;
            action_type = type;
        }

        public void Clear()
        {
            schedules.Clear();
        }

        public void Cleanup()
        {
            List<ViewItem> rm = new List<ViewItem>();
            foreach (ViewItem p in schedules)
            {
                if (!ShouldContains(p))
                {
                    rm.Add(p);
                }
            }
            foreach (ViewItem p in rm)
            {
                schedules.Remove(p); ;
            }
        }

        public void Add(ViewItem p)
        {
            schedules.Add(p);
        }

        public bool ShouldContains(ViewItem vi)
        {
            Project p = vi.Project;
            if (Common)
            {
                if (p.PlanSchedule.ContainsKey(Schedule.HalfMonth))
                {
                    Schedule s = p.PlanSchedule[Schedule.HalfMonth];
                    int s0 = DateUtils.GetDateSigniture(dt);
                    int s1 = DateUtils.GetDateSigniture(s.PlanDate);
                    return s1 == s0;
                }
            }
            else
            {
                if (p.PlanSchedule.ContainsKey(Schedule.WholeYear))
                {
                    Schedule s = p.PlanSchedule[Schedule.WholeYear];
                    return dt == s.PlanDate;
                }
            }
            return false;
        }

        public void ApplyToProject(ViewItem vi)
        {
            Project p = vi.Project;
            if (ShouldContains(vi))
                return;
            if (Common)
            {
                Schedule s = new Schedule();
                s.PlanDate = dt;
                s.Data.ProjectId = p.Id;
                s.Data.ActionType = Schedule.HalfMonth;
                s.Proj = p;
                HitachiServiceClient client = WCFClient.GetClient();
                if (p.PlanSchedule.ContainsKey(Schedule.HalfMonth))
                {
                    Schedule old = p.PlanSchedule[Schedule.HalfMonth];
                    client.FreezeScheduleAsync(old.Data);
                }
                p.PlanSchedule[Schedule.HalfMonth] = s;
                client.SaveScheduleAsync(s.Data);
            }
            else
            {
                Schedule s = new Schedule();
                s.PlanDate = dt;
                s.Data.ProjectId = p.Id;
                s.Data.ActionType = Schedule.WholeYear;
                s.Proj = p;
                HitachiServiceClient client = WCFClient.GetClient();
                if (p.PlanSchedule.ContainsKey(Schedule.WholeYear))
                {
                    Schedule old = p.PlanSchedule[Schedule.WholeYear];
                    client.FreezeScheduleAsync(old.Data);
                }
                p.PlanSchedule[Schedule.WholeYear] = s;
                client.SaveScheduleAsync(s.Data);
            }
            RaisePropertyChanged("UnitTimeProjects");
            parent.Cleanup();
        }

        public void RemoveProject(ViewItem vi)
        {
            Project p = vi.Project;
            HitachiServiceClient client = WCFClient.GetClient();
            if (Common)
            {
                if (p.PlanSchedule.ContainsKey(Schedule.HalfMonth))
                {
                    Schedule s = p.PlanSchedule[Schedule.HalfMonth];
                    client.FreezeScheduleAsync(s.Data);
                }
            }
            else
            {
                if (p.PlanSchedule.ContainsKey(Schedule.WholeYear))
                {
                    Schedule s = p.PlanSchedule[Schedule.WholeYear];
                    client.FreezeScheduleAsync(s.Data);
                }
            }
        }

        public void RefreshData()
        {
            RaisePropertyChanged("UnitTimeProjects");
        }

        #region PropertChanged Block
        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
        #endregion
    }
}
